package org.easy.core.util;

import java.math.BigDecimal;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;


public class StringUtil {

    /**
     * 空字符串
     */
    private static final String NULLSTR = "";

    /**
     * 获取字符串长度，中文算2位
     *
     * @param s
     * @return int
     */
    public static int getWordCount(String s) {
        int length = 0;
        for (int i = 0; i < s.length(); i++) {
            int ascii = Character.codePointAt(s, i);
            if (ascii >= 0 && ascii <= 255) {
                length++;
            } else {
                length += 2;
            }
        }
        return length;
    }

    /**
     * 获取文件的扩展名
     *
     * @param filename
     * @return
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /**
     * 获取文件名，不包括扩展名
     *
     * @param filename
     * @return
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    /**
     * 去前后字符
     *
     * @param str
     * @param reg
     * @return String
     */
    public static String trimStr(String str, String reg) {

        if (str.indexOf(reg) == 0) {
            str = str.substring(1);
        }

        if (str.lastIndexOf(reg) > 0) {
            str = str.substring(0, str.length() - 1);
        }

        return str;
    }

    /**
     * 去前字符
     *
     * @param str
     * @param reg
     * @return String
     */
    public static String trimStStr(String str, String reg) {

        if (str.indexOf(reg) == 0) {
            str = str.substring(1);
        }

        return str;
    }

    /**
     * 去后字符
     *
     * @param str
     * @param reg
     * @return String
     */
    public static String trimEdStr(String str, String reg) {

        if (str.lastIndexOf(reg) > 0) {
            str = str.substring(0, str.length() - 1);
        }

        return str;
    }

    /**
     * 32位随机数字。据说千年不重复
     *
     * @return String
     */
    public static String GetGUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 产生4位随机数(0000-9999)
     *
     * @return 4位随机数
     */
    public static String getFourRandom() {
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++) {
                fourRandom = "0" + fourRandom;
            }
        }
        return fourRandom;
    }

    /**
     * 产生6位随机数(000000-999999)
     *
     * @return 6位随机数
     */
    public static String getSixRandom() {
        Random random = new Random();
        String fourRandom = random.nextInt(1000000) + "";
        int randLength = fourRandom.length();
        if (randLength < 6) {
            for (int i = 1; i <= 6 - randLength; i++) {
                fourRandom = "0" + fourRandom;
            }
        }
        return fourRandom;
    }

    /**
     * 类型转换 Obj 转String
     *
     * @return String
     */
    public static String getStringByObject(Object obj) {
        String str = null;
        if (obj instanceof String) {
            str = (String) obj;
        } else if (obj instanceof Integer) {
            str = String.valueOf((Integer) obj);
        }
        return str;
    }

    /**
     * 类型转换 Obj 转Integer
     *
     * @return Integer
     */
    public static Integer getIntegerByObject(Object obj) {
        Integer num = null;
        if (obj instanceof Integer) {
            num = (Integer) obj;
        } else if (obj instanceof String) {
            if (obj != "") {
                num = Integer.valueOf((String) obj);
            }
        }
        return num;
    }

    /**
     * 类型转换 Obj 转BigDecimal
     *
     * @return BigDecimal
     */
    public static BigDecimal getBigDecimalByObject(Object obj) {
        BigDecimal decimal = null;
        if (obj instanceof Integer) {
            decimal = new BigDecimal((Integer) obj);
        } else if (obj instanceof Double) {
            decimal = new BigDecimal((Double) obj);
        }
        return decimal;
    }

    /**
     * 类型转换 Obj 转Byte
     *
     * @return Integer
     */
    public static Byte getByteByObject(Object obj) {
        Byte b = null;
        if (obj instanceof String) {
            b = Byte.valueOf((String) obj);
        } else if (obj instanceof Integer) {
            b = ((Integer) obj).byteValue();
        }
        return b;
    }

    /**
     * 随机数字+英文
     *
     * @param length 为几位密码
     * @return String
     */
    public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        // length为几位密码
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 随机数字+英文
     *
     * @param length 为几位密码
     * @return String
     */
    public static String getLowerStringRandom(int length) {
        String val = "";
        Random random = new Random();
        // length为几位密码
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 输出是大写字母还是小写字母
//                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + 'a');
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 整形集合转字符串
     *
     * @param list
     * @return
     */
    public static String intlistToString(List<Integer> list) {
        if (list == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean first = true;
        // 第一个前面不拼接","
        for (Integer num : list) {
            if (first) {
                first = false;
            } else {
                result.append(",");
            }
            result.append(String.valueOf(num));
        }
        return result.toString();
    }

    /**
     * 字符串集合转字符串
     *
     * @param list
     * @return
     */
    public static String StringlistToString(List<String> list) {
        if (list == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean first = true;
        // 第一个前面不拼接","
        for (String num : list) {
            if (first) {
                first = false;
            } else {
                result.append(",");
            }
            result.append(String.valueOf(num));
        }
        return result.toString();
    }

    /**
     * sql模糊查询特殊字符(\,%和_)处理
     *
     * @return string
     */
    public static String escapeChar(String before) {
        if (before != null && before.trim().length() > 0) {
            before = before.replaceAll("\\\\", "\\\\\\\\");
            before = before.replaceAll("_", "\\\\_");
            before = before.replaceAll("%", "\\\\%");
        }
        return before;
    }

    /**
     * 字符串转List<Long>
     *
     * @return string
     */
    public static List<Long> stringToListLong(String Str) {
        if (ObjectUtil.isEmpty(Str) || ObjectUtil.isEmpty(Str.trim())) {
            return new ArrayList<>();
        }
        List<Long> list = Arrays.asList(Str.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        return list;
    }

    /**
     * 字符串转Set<Long>
     *
     * @param Str
     * @return
     */
    public static Set<Long> stringToSetLong(String Str) {
        return Arrays.asList(Str.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toSet());
    }


    public static void main(String[] args) {
        String stringRandom = getLowerStringRandom(4);
        System.out.println(stringRandom);
    }


}
